<template>
  <div>
    <v-centerForm>
      <template slot='left'>
        <el-form inline :model="searchObject" ref="searchObject" label-width="50px">
          <el-form-item label="表名" prop="tableName">
            <el-input placeholder="请输入表名" v-model="searchObject.tableName"></el-input>
          </el-form-item>
          <el-button size="mini" icon="el-icon-search" type="primary" @click="search">搜索</el-button>
          <el-button size="mini" icon="el-icon-refresh-left" @click="resetSearch">重置</el-button>
        </el-form>
      </template>
      <template slot='right'>
        <el-button size="mini" icon="el-icon-download" type="success" @click="generateBatchDialog"
                   :disabled="tableSelectData.length === 0">生成代码
        </el-button>
      </template>
    </v-centerForm>
    <x-table :loading="loadingBtn.tableLoading" :columnData="columnData" :tableData="tableData"
             :tableSize="pagination.size" :tableTotal="pagination.total" @currentPage="getCurrent" @pageSize="getSize"
             @selectionChange="selectionChange">
      <el-table-column slot="selection" :selectable="row=>!row.init" type="selection" align="center">
      </el-table-column>
    </x-table>
  </div>
</template>

<script>

import generatorApi from '@/api/system/generatorApi'
import xTable from '@/components/xTable/x-table'
import { axiosBaseUrl } from '@/common/config/baseConfig'

export default {
  name: 'generatorList',
  components: { xTable },
  data () {
    return {
      loadingBtn: {
        tableLoading: false
      },
      searchObject: {
        tableName: ''
      },
      tableData: [],
      tableSelectData: [],
      columnData: [
        {
          slot: 'selection'
        },
        {
          type: 'index',
          label: '序号'
        },
        {
          label: '表名',
          prop: 'tableName'
        },
        {
          label: '表注释',
          prop: 'tableComment'
        },
        {
          label: '存储引擎',
          prop: 'engine'
        },
        {
          label: '创建时间',
          prop: 'createTime'
        }
      ],
      rules: {
        tableName: [
          {
            required: true,
            message: '表名不能为空',
            trigger: 'blur'
          }
        ],
        tableComment: [
          {
            required: true,
            message: '表注释不能为空',
            trigger: 'blur'
          }
        ],
      },
      pagination: {
        current: 1,
        size: 10,
        total: 0
      },
      dialog: {
        title: '',
        flag: ''
      },
      dialogVisible: false,
      form: {},
      formLabelWidth: '100px'
    }
  },
  created () {
    this.selectPage()
  },
  methods: {
    search () {
      this.selectPage()
    },
    resetSearch () {
      this.searchObject = {}
      this.selectPage()
    },
    getCurrent (value) {
      this.pagination.current = value
      this.selectPage()
    },
    getSize (value) {
      this.pagination.size = value
      this.selectPage()
    },
    async selectPage () {
      this.loadingBtn.tableLoading = true
      const {
        code,
        data,
        msg
      } = await generatorApi.selectPageOfGenerator(this.pagination.current, this.pagination.size, this.searchObject)
      if (code === this.$constant.SUCCESS) {
        this.tableData = data.list
        this.pagination.total = data.totalCount
        this.loadingBtn.tableLoading = false
      } else {
        this.$message.error(msg)
      }
    },
    selectionChange (value) {
      this.tableSelectData = value
    },
    generateBatchDialog () {
      this.$confirm('是否确认生成代码？', '系统提示').then(_ => {
        this.generateBatch()
      }).catch(_ => {
      })
    },
    generateBatch () {
      const tableNameList = this.tableSelectData.map(item => item.tableName)
      window.open(axiosBaseUrl + `/api/v1/generator/generateBatch?tableNameList=${tableNameList}`)
    }
  }
}
</script>

<style scoped>

</style>
